package com.ssmIntegration.controller;


import com.ssmIntegration.pojo.Schedule;
import com.ssmIntegration.service.ScheduleService;
import com.ssmIntegration.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/schedule")
@Slf4j
/*
同源策略：比较访问方和被访问方的域名，如果协议、ip、端口都相同即同源
同源策略规定同源的资源才能访问
 */
// @CrossOrigin注解，允许其它源访问我们的controller，也只写在方法上
@CrossOrigin
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @GetMapping("/{pageSize}/{currentPage}")
    public R page(@PathVariable int pageSize, @PathVariable int currentPage) {
        R r = scheduleService.page(pageSize, currentPage);
        // slf4j，跟着敲的，没懂在哪里运行了这个
        log.info("查询的数据为：{}", r);
        return r;
    }

    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable int id) {
        R r = scheduleService.deleteById(id);
        return r;

    }

    @PostMapping
    public R save(@Validated @RequestBody Schedule schedule, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("参数为null，不可保存!");
        }
        R r = scheduleService.save(schedule);
        return r;
    }

    @PutMapping
    public R update(@Validated @RequestBody Schedule schedule, BindingResult result) {
        if (result.hasErrors()) {
            return R.fail("参数为null，不可修改!");
        }
        R r = scheduleService.update(schedule);
        return r;
    }


}
